<?php
/*
 * @component AlphaGetCouponCode
 * @copyright Copyright (C) 2008-2009 Bernard Gilly
 * @license : GNU/GPL
 * @Website : http://www.alphaplug.com
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');
jimport( 'joomla.mail.helper' );
/**
 * @package AlphaGetCouponCode
 */
class AlphagetcouponcodeController extends JController
{
	/**
	 * Custom Constructor
	 */
 	function __construct()	
	{
		parent::__construct( );
	}
	
	function getcouponcode($params=null) 
	{
		global $mainframe;
		
		$model      = &$this->getModel ( 'alphagetcouponcode' );
		$view       = $this->getView  ( 'getcouponcode','html' );
		
		$msg = "";
		
		// check real Itemid
		$rmenuid	= $model->_getRealItemid();	
		
		if ( !$params )
		{
			//$params = $model->_getParamsAGCC();
			$params = $model->_getParamsAGCC2($rmenuid);
		}
		
		// START CHECKING
						
		// AlphaUserPoints component is available and installed ?		
		$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
		if ( file_exists($api_AUP))
		{
			require_once ($api_AUP);
		}
		else 
		{
			$msg = JText::_('AGCC_ALPHAUSERPOINTS_IS_NOT_INSTALLED' );
		}
		
		// rule is enabled ?				
		if ( !AlphaUserPointsHelper::checkRuleEnabled( 'plgaup_alphagetcouponcode_vm' ) ) 
		{
			$msg = JText::_('AGCC_THISRULEDISABLED' );
		}
		
		// VirtueMart component is available and installed ?		
		if ( ! $model->_checkVirtueMart() ) $msg = JText::_('AGCC_VIRTUEMART_NOT_INSTALLED' );
		// get all coupon code published in VirtueMart		
		$couponscode = $model->_getCouponCodeVM( $params->get('coupontype', 0) );
		if ( !$couponscode ) $msg = JText::_('AGCC_NO_COUPON_CODE_AVAILABLE' );		
		
		// Check if user logged
		$user = & JFactory::getUser();
		
		if ( !$user->id ) 
		{
			$msg = JText::_('AGCC_YOUMUSTBELOGGED' );		
		}
		
		if ( $msg ) $mainframe->redirect('index.php', $msg, 'error');
		
		// END CHECKING				
		
		// active user		
		$referrerid = $model->_checkUser();

		// get current total points
		$currenttotalpoints = AlphaUserPointsHelper::getCurrentTotalPoints( $referrerid );
		
		// get article for description if exist
		$article = $model->_getArticleDescription( $params->get('id_content_to_show') );
		$title = "";
		$description = "";
		
		if ( $article ) 
		{		
			$title = $article[0]->title;
			$description = $article[0]->introtext;
		}
		
		$useremail = $user->email;		
		
		$view->assign( 'params',                         $params );
		$view->assign( 'referreid',                  $referrerid );
		$view->assign( 'userid',                       $user->id );
		$view->assign( 'currenttotalpoints', $currenttotalpoints );
		$view->assign( 'menuid',                         $menuid );
		$view->assign( 'menuid',                        $rmenuid );
		
		$view->assign( 'menuname',                     $menuname );
		$view->assign( 'title',                           $title );
		$view->assign( 'description',               $description );
		$view->assign( 'useremail',                   $useremail );
		$view->assign( 'couponscode',               $couponscode );
		$view->assign( 'success',                              0 );
		$view->assign( 'msg',                                 '' );
	
		// Display
		$view->_display();
	}	
	
	function purchase() 
	{
		global $mainframe;
		
		// AlphaUserPoints component		
		require_once (JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php');
		
		// Check if user logged
		$user = & JFactory::getUser();		
		
		$idcoupon		= JRequest::getVar('id', 0, 'post', 'int');	
		$points			= JRequest::getVar('points', 0, 'post', 'int');
		$coupontype		= JRequest::getVar('type', 'gift', 'post', 'string');
	
		$model      = &$this->getModel ( 'alphagetcouponcode' );
		$view       = $this->getView  ( 'getcouponcode','html' );
		
		// check real Itemid
		$rmenuid	= $model->_getRealItemid();
		
		if ( $rmenuid && $menuid!=$rmenuid ) {
			$menuid = $rmenuid;
		} elseif ( !$rmenuid ){
			return;
		} 		
		
		// active user		
		$referrerid = $model->_checkUser();		
		
		// Get the parameters of the active menu item
		$menus	    = &JSite::getMenu();
		$menu       = $menus->getActive();
		$menuid     = $menu->id;
		$menuname   = $menu->name;		
		
		$params = $model->_getParamsAGCC();
		
		$code_coupon = $model->_purchase_coupon( $referrerid, $idcoupon, $points, $coupontype );
		
		// get current total points
		$currenttotalpoints = AlphaUserPointsHelper::getCurrentTotalPoints( $referrerid );

		$view->assign( 'params', $params );
		$view->assign( 'menuname', $menuname );
		$view->assign( 'referreid', $referrerid );
		$view->assign( 'menuid', $menuid );
		$view->assign( 'menuname', $menuname );
		$view->assign( 'success', 1 );
		$view->assign( 'currenttotalpoints', $currenttotalpoints );
		$view->assign( 'codecoupon', $code_coupon );
		$view->assign( 'useremail', $useremail );
		
		// Display
		$view->_display();
	}
}
?>